package view;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JTextField;

import model.State;

/**
 * Class to validate that a name typed in is actually a user in our system.
 * @author Sarah George
 * @version Autumn 2012
 */
@SuppressWarnings("serial")
public class ValidateAction extends AbstractAction {
	
	/**
	 * Field to hold the JTextField.
	 */
	private JTextField my_field;
	
	/**
	 * Field to hold the text typed into the text field.
	 */
	private String my_name;
	
	/**
	 * Field to store the refence to the current state object.
	 */
	private State my_state;
	
	/**
	 * Field to store the JFrame.
	 */
	private JFrame my_frame;
	
	/**
	 * Constructor for a Validate Action.
	 * @param the_field the text field
	 * @param the_state the current state of the program
	 * @param the_frame the JFrame in which this lives.
	 */
	public ValidateAction(JTextField the_field, State the_state, JFrame the_frame) {
		super("Validate");
	    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_V);
	    putValue(Action.SELECTED_KEY, true);
	    my_field = the_field;
	    my_state = the_state;
	    my_frame = the_frame;
	}

	/**
	 * Method to validate the name typed in to the JTextField.
	 * @param arg0 the action event that triggers this method.
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		my_name = my_field.getText();
		my_state.setName(my_name);
		my_state.validateUser();
		my_frame.repaint();
	}
}//End class Validate Action.